home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / BITMAP.ZIP / PRINT.C < prev    next >
C/C++ Source or Header  |  1993-04-19  |  8KB  |  345 lines

  1. /*
  2. **    $id: ssvcid print.c 1.1 08/03/92 10:01 am$
  3. **        This file contains the functions needed to use the printer setup and print common dialogs.
  4. **
  5. **    (C) 1991-3    Larry Widing
  6. */
  7. #include    <windows.h>
  8. #include    <commdlg.h>
  9. #include    <drivinit.h>
  10. #include    <bwcc.h>
  11. #include    <string.h>
  12. #include    <malloc.h>
  13. #include    "bitmaps.h"
  14. #include    "bmmanip.h"
  15. #include    "print.h"
  16.  
  17. /*
  18. **    Local variables
  19. */
  20. static HBITMAP    helpBitmaps[3];
  21. static HBITMAP    setupBitmaps[3];
  22. static HBITMAP    optionBitmaps[3];
  23. static UINT        scale = 100;
  24. static UINT        scaleMode = 0;
  25. static HGLOBAL    DevMode;
  26. static HGLOBAL    DevNames;
  27.  
  28. /*
  29. **
  30. */
  31. void
  32. PrintCleanup(void)
  33. {
  34.     if (DevMode != (HGLOBAL)NULL)
  35.     {
  36.         GlobalFree(DevMode);
  37.     }
  38.  
  39.     if (DevNames != (HGLOBAL)NULL)
  40.     {
  41.        GlobalFree(DevNames);
  42.    }
  43. }
  44.  
  45. /*
  46. **    UINT CALLBACK EXPORT            FALSE if common dialog handler should process message
  47. **    PrintHook(
  48. **      HWND dlg,                        Handle of diaog window
  49. **      UINT message,                Window's message being passed
  50. **      WPARAM wParam,                Word parameter
  51. **      LPARAM lParam);                Long parameter
  52. **
  53. **    Filter the messages being sent to the common print dialog to support the following
  54. **    extended functions:
  55. **            Bitmapped buttons
  56. **            Gray coloring
  57. **
  58. ** Modification History:
  59. ** 07/22/92  LCW  Created
  60. */
  61. UINT CALLBACK EXPORT
  62. PrintHook(HWND dlg, UINT message, WPARAM wParam, LPARAM lParam)
  63. {
  64.     BOOL    rc = FALSE;
  65.    int    i;
  66.  
  67.     switch (message)
  68.     {
  69.         case WM_COMMAND:
  70.             switch (wParam)
  71.             {
  72.                 case 2000:
  73.                 case 2001:
  74.                 case 2002:
  75.                     EnableWindow(GetDlgItem(dlg, 2003), (wParam == 2002) ? TRUE : FALSE);
  76.                scaleMode = wParam - 2000;
  77.                     rc = TRUE;
  78.                     break;
  79.  
  80.                 case IDOK:
  81.                     scale = GetDlgItemInt(dlg, 2003, &rc, FALSE);
  82.                     if (!rc)
  83.                         scale = 100;
  84.                     rc = FALSE;
  85.                     break;
  86.             }
  87.             break;
  88.  
  89.         case WM_CTLCOLOR:
  90.             switch (HIWORD(lParam))
  91.             {
  92.                 case CTLCOLOR_BTN:
  93.                 case CTLCOLOR_SCROLLBAR:
  94.                 case CTLCOLOR_LISTBOX:
  95.                     break;
  96.  
  97.                 case CTLCOLOR_DLG:
  98.                 case CTLCOLOR_EDIT:
  99.                 case CTLCOLOR_MSGBOX:
  100.             case CTLCOLOR_STATIC:
  101.                     SetBkColor((HDC)wParam, RGB(192,192,192));
  102.                SetTextColor((HDC)wParam, RGB(0,0,0));
  103.                 return (BOOL)(HIWORD(lParam) == CTLCOLOR_DLG ? DialogBrush : GrayBrush);
  104.             }
  105.             break;
  106.  
  107.         case WM_INITDIALOG:
  108.         {
  109.             helpBitmaps[0] = LoadBitmap(AppInstance, MAKEINTRESOURCE(100));
  110.             helpBitmaps[1] = LoadBitmap(AppInstance, MAKEINTRESOURCE(101));
  111.             helpBitmaps[2] = LoadBitmap(AppInstance, MAKEINTRESOURCE(102));
  112.             SendDlgItemMessage(dlg, 1038, BBM_SETBITS, 0,
  113.                 (LONG)(LPSTR)helpBitmaps);
  114.             SetDlgItemText(dlg, 1038, "");
  115.  
  116.             setupBitmaps[0] = LoadBitmap(AppInstance, MAKEINTRESOURCE(103));
  117.             setupBitmaps[1] = LoadBitmap(AppInstance, MAKEINTRESOURCE(104));
  118.             setupBitmaps[2] = LoadBitmap(AppInstance, MAKEINTRESOURCE(105));
  119.             SendDlgItemMessage(dlg, 1024, BBM_SETBITS, 0,
  120.                 (LONG)(LPSTR)setupBitmaps);
  121.             SetDlgItemText(dlg, 1024, "");
  122.  
  123.             CheckDlgButton(dlg, 2000, TRUE);
  124.          EnableWindow(GetDlgItem(dlg, 2003), FALSE);
  125.             break;
  126.       }
  127.  
  128.         case WM_DESTROY:
  129.             for (i = 0 ; i < 3 ; ++i)
  130.          {
  131.                 if (helpBitmaps[i] != (HBITMAP)NULL)
  132.                 {
  133.                     DeleteObject(helpBitmaps[i]);
  134.                helpBitmaps[i] = (HBITMAP)NULL;
  135.                 }
  136.                 if (setupBitmaps[i] != (HBITMAP)NULL)
  137.                 {
  138.                     DeleteObject(setupBitmaps[i]);
  139.                setupBitmaps[i] = (HBITMAP)NULL;
  140.             }
  141.          }
  142.          break;
  143.    }
  144.  
  145.     return rc;
  146. }
  147.  
  148. /*
  149. **    UINT CALLBACK EXPORT            FALSE if common dialog handler should process message
  150. **    PrintSetupHook(
  151. **      HWND dlg,                        Handle of diaog window
  152. **      UINT message,                Window's message being passed
  153. **      WPARAM wParam,                Word parameter
  154. **      LPARAM lParam);                Long parameter
  155. **
  156. **    Filter the messages being sent to the common printer setup dialog to support the following
  157. **    extended functions:
  158. **            Bitmapped buttons
  159. **            Gray coloring
  160. **
  161. ** Modification History:
  162. ** 07/22/92  LCW  Created
  163. */
  164. UINT CALLBACK EXPORT
  165. PrintSetupHook(HWND dlg, UINT message, WPARAM wParam, LPARAM lParam)
  166. {
  167.     BOOL    rc = FALSE;
  168.    int    i;
  169.  
  170.     switch (message)
  171.     {
  172.         case WM_CTLCOLOR:
  173.             switch (HIWORD(lParam))
  174.             {
  175.                 case CTLCOLOR_BTN:
  176.                 case CTLCOLOR_SCROLLBAR:
  177.                 case CTLCOLOR_LISTBOX:
  178.                     break;
  179.  
  180.                 case CTLCOLOR_DLG:
  181.                 case CTLCOLOR_EDIT:
  182.                 case CTLCOLOR_MSGBOX:
  183.             case CTLCOLOR_STATIC:
  184.                     SetBkColor((HDC)wParam, RGB(192,192,192));
  185.                SetTextColor((HDC)wParam, RGB(0,0,0));
  186.                 return (BOOL)(HIWORD(lParam) == CTLCOLOR_DLG ? DialogBrush : GrayBrush);
  187.             }
  188.             break;
  189.  
  190.         case WM_INITDIALOG:
  191.         {
  192.             if (helpBitmaps[0] == (HBITMAP)NULL)
  193.          {
  194.                 helpBitmaps[0] = LoadBitmap(AppInstance, MAKEINTRESOURCE(100));
  195.                 helpBitmaps[1] = LoadBitmap(AppInstance, MAKEINTRESOURCE(101));
  196.                 helpBitmaps[2] = LoadBitmap(AppInstance, MAKEINTRESOURCE(102));
  197.          }
  198.             SendDlgItemMessage(dlg, 1038, BBM_SETBITS, 0,
  199.                 (LONG)(LPSTR)helpBitmaps);
  200.             SetDlgItemText(dlg, 1038, "");
  201.  
  202.             optionBitmaps[0] = LoadBitmap(AppInstance, MAKEINTRESOURCE(106));
  203.             optionBitmaps[1] = LoadBitmap(AppInstance, MAKEINTRESOURCE(107));
  204.             optionBitmaps[2] = LoadBitmap(AppInstance, MAKEINTRESOURCE(108));
  205.             SendDlgItemMessage(dlg, 1024, BBM_SETBITS, 0,
  206.                 (LONG)(LPSTR)optionBitmaps);
  207.             SetDlgItemText(dlg, 1024, "");
  208.             break;
  209.       }
  210.  
  211.         case WM_DESTROY:
  212.             for (i = 0 ; i < 3 ; ++i)
  213.          {
  214.                 if (helpBitmaps[i] != (HBITMAP)NULL && setupBitmaps[0] == (HBITMAP)NULL)
  215.                 {
  216.                     DeleteObject(helpBitmaps[i]);
  217.                helpBitmaps[i] = (HBITMAP)NULL;
  218.                 }
  219.                 if (optionBitmaps[i] != (HBITMAP)NULL)
  220.                 {
  221.                     DeleteObject(optionBitmaps[i]);
  222.                     optionBitmaps[i] = (HBITMAP)NULL;
  223.             }
  224.          }
  225.          break;
  226.    }
  227.  
  228.     return rc;
  229. }
  230.  
  231. /*
  232. ** void
  233. ** PrintImage(
  234. **   HWND wnd,                Handle of parent window
  235. **   int setupOnly);        TRUE if only the printer setup should be displayed
  236. **
  237. **    Print the current image, or display the printer setup to the user.
  238. **
  239. ** Modification History:
  240. ** 07/24/92  LCW  Created
  241. */
  242. void
  243. PrintImage(HWND wnd, int setupOnly)
  244. {
  245.     PRINTDLG        prdata;
  246.  
  247.     /*
  248.     **    Initialize the prdata structure
  249.     */
  250.     memset(&prdata, 0, sizeof(prdata));
  251.     prdata.lStructSize = sizeof(prdata);
  252.     prdata.hwndOwner = wnd;
  253.     prdata.hInstance = AppInstance;
  254.     prdata.lpPrintTemplateName = MAKEINTRESOURCE(1538);
  255.     prdata.lpfnPrintHook = PrintHook;
  256.     prdata.lpSetupTemplateName = MAKEINTRESOURCE(1539);
  257.     prdata.lpfnSetupHook = PrintSetupHook;
  258.  
  259.     if (setupOnly)
  260.    {
  261.         prdata.Flags = PD_PRINTSETUP | PD_SHOWHELP;
  262.     }
  263.     else
  264.     {
  265.         prdata.Flags = PD_RETURNDC | PD_ENABLEPRINTTEMPLATE | PD_ENABLEPRINTHOOK
  266.             | PD_NOPAGENUMS | PD_NOSELECTION;
  267.     }
  268.     prdata.Flags |= PD_ENABLESETUPTEMPLATE | PD_ENABLESETUPHOOK;
  269.  
  270.     /*
  271.     **    Display the printing dialogs
  272.     */    
  273.     if (PrintDlg(&prdata))
  274.     {
  275.         if (!setupOnly)
  276.         {
  277.             HDIB        handle = DIBitmapHandle;
  278.             HPALETTE palette = DibPalette;
  279.  
  280.             if (handle == (HDIB)NULL)
  281.             {
  282.                 /*
  283.              **    Create a DIB to print
  284.                 */
  285.                 extern int    FileSaveMode;
  286.  
  287.                 handle = BitmapToDIB(BitmapHandle, FileSaveMode);
  288.              palette = CreateDibPalette(handle);
  289.             }
  290.  
  291.             Escape(prdata.hDC, STARTDOC, 8, "Bitmaps", NULL);
  292.  
  293.             switch (scaleMode)
  294.             {
  295.              case 0:
  296.                     DrawDIBitmap(prdata.hDC, 0, 0, handle, palette);
  297.                     break;
  298.  
  299.                 case 1:
  300.                     StretchDIBitmap(prdata.hDC, 0, 0,
  301.                         GetDeviceCaps(prdata.hDC, HORZRES),
  302.                         GetDeviceCaps(prdata.hDC, VERTRES),
  303.                         handle, palette);
  304.                     break;
  305.  
  306.                 case 2:
  307.                     StretchDIBitmap(prdata.hDC, 0, 0, 0, scale,
  308.                         handle, palette);
  309.                break;
  310.             }
  311.  
  312.             Escape(prdata.hDC, NEWFRAME, 0, NULL, NULL);
  313.             Escape(prdata.hDC, ENDDOC, 0, NULL, NULL);
  314.  
  315.             DeleteDC(prdata.hDC);
  316.             if (handle != DIBitmapHandle)
  317.             {
  318.               GlobalFree(handle);
  319.             }
  320.             if (palette != DibPalette)
  321.             {
  322.               DeleteObject(palette);
  323.             }
  324.         } 
  325.         if (prdata.hDevMode != NULL)
  326.       {
  327.             GlobalFree(prdata.hDevMode);
  328.       }
  329.         if (prdata.hDevNames != NULL)
  330.       {
  331.             GlobalFree(prdata.hDevNames);
  332.       }
  333.     }
  334. }
  335.  
  336. /*
  337. **    Modification